"""
Case Type   : 小型化
Case Name   : gs_dump时，管道输入密码错误
Create At   : 2025/2/7
Owner       : wan005
Description :
    1.创建用户，准备数据
    2.gs_dump时，管道输入密码错误
Expect      :
    1.数据准备完成
    2.导出失败，提示密码错误
History     :
"""
import os
import unittest
from testcase.utils.ComInstall import LiteInstall, LiteUnInstall
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class Lite(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.com_install = LiteInstall(self.pkg_path, None, None)
        self.com_uninstall = LiteUnInstall(self.pkg_path)
        self.common = Common()
        self.constant = Constant()
        self.root_node = Node('PrimaryRoot')
        self.user_node = Node('PrimaryDbUser')
        self.user = None
        self.port = None
        self.table = 't_lite_001'
        self.com_user = 'u_lite_001'

    def test_pipeline(self):
        text = '----prestep1:获取可用用户和端口;expect:获取成功----'
        self.log.info(text)
        self.user, self.port = self.com_install.get_user_and_port(self.root_node)
        self.log.info(f'self.user:{self.user}, self.port:{self.port}')
        self.user_node.ssh_user = self.user
        self.user_node.ssh_password = macro.COMMON_PASSWD
        self.data_path = os.path.join('/home', self.user, 'data')
        self.app_path = os.path.join('/home', self.user, 'app')
        self.audit_path = os.path.join(self.data_path, 'pg_audit')
        self.com_install.data_path = self.data_path
        self.com_install.app_path = self.app_path

        text = '----prestep2:创建数据库用户和用户组;expect:创建成功----'
        self.log.info(text)
        self.com_install.prepare_user(self.user, self.root_node)

        text = '----prestep3:获取数据库安装包并解压;expect:成功----'
        self.log.info(text)
        res = self.com_install.tar_pkg(self.root_node)
        self.assertIn('install.sh', res, f'执行失败: {text}')

        text = '----prestep4:执行脚本安装数据库;expect:安装成功----'
        self.log.info(text)
        res = self.com_install.install_single(self.user_node, self.port)
        self.log.info(res)
        self.assertIn('start datanode success', res, f'执行失败: {text}')

        text = '----step1:准备数据;expect:成功---'
        self.log.info(text)
        sql_cmd1 = f'create user {self.com_user} password ' \
                   f'\'{macro.COMMON_PASSWD}\';' \
                   f'create table {self.table} (id int);' \
                   f'insert into {self.table} values(generate_series(1,10));'
        exe_cmd1 = f'gsql -d postgres -p {self.port} -c "{sql_cmd1}"'
        self.log.info(exe_cmd1)
        res1 = self.user_node.sh(exe_cmd1).result()
        self.log.info(res1)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, res1,
                      '执行失败:' + text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, res1,
                      '执行失败:' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, res1,
                      '执行失败:' + text)

        text = '----step2:gs_dump时，管道输入密码错误;expect:失败----'
        self.log.info(text)
        error_passwd = macro.COMMON_PASSWD + '_error'
        exe_cmd2 = f'echo "{error_passwd}" | gs_dump -p {self.port} ' \
                   f'postgres -U {self.com_user} ' \
                   f'-f {os.path.join(self.app_path, "test.sql")} --pipeline'
        self.log.info(exe_cmd2)
        res2 = self.user_node.sh(exe_cmd2).result()
        self.log.info(res2)
        self.assertIn('Invalid username/password,login denied', res2,
                      '执行失败:' + text)

    def tearDown(self):
        text = '-----清理环境-----'
        self.log.info(text)
        res = self.com_uninstall.uninstall(self.user_node)
        self.com_uninstall.clear_env(self.root_node)
        self.assertIn('success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
